<?php
/*
 * 商品的模型
 */
class GoodsModel extends Model {
	public $validate = array( 
		array('goods_name', 'nonull', '商品名不能为空', 2, 3),
		array('shop_price', 'nonull', '商品单价不能为空', 2, 3),
		array('goods_cateid', 'nonull', '商品分类不能为空', 2, 3),
		
//		array()
		 );

	/**
	 * 获得当前商品的详细信息
	 */
	public function getGoodsDetail($gid) {
		return $this -> join("__goods__ AS g LEFT JOIN __goods_detailes__ as gd ON gd.goods_gid=g.gid") -> where("g.gid={$gid}") -> find();
	}

	/**
	 * 获得当前商品的属性
	 */
	public function getCurrentAttr($gid, $type) {
		if ($type == 1) {
			$result = $this -> join("__typeattr__ t JOIN __goods_attr__ ga ON ga.typeattr_id=t.id") -> where("ga.goods_gid={$gid} AND t.attrtype={$type}") -> all();
			foreach ($result as $key => $val) {
				$result[$key]['attrvalue'] = explode(',', $val['attrvalue']);
			}
		}else{
			$result = $this -> field('attr_value,goods_gid,typeattr_id') -> join("__typeattr__ t JOIN __goods_attr__ ga ON ga.typeattr_id=t.id") -> where("ga.goods_gid={$gid} AND t.attrtype={$type}") -> all();
		}
		return $result;
	}

	/**
	 * 添加商品方法
	 */
	public function addGoods($Data) {
		if (!$this -> create($Data))
			return false;
		//返回商品表的自添ID
		$gid = $this -> add();
		if (!$gid) {
			$this -> error = '对不起商品添加失败';
			return false;
		} else {
			return $gid;
		}
	}

	/**
	 * 返回一个可以插入到商品表的数组
	 */
	public function goodsTable($data) {
		//商品名
		$goodsData['goods_name'] = $data['goods_name'];
		//商品货号
		$goodsData['goods_sn'] = $data['goods_sn'];
		//商品单位
		$goodsData['goods_unit'] = $data['goods_unit'];
		//市场价
		$goodsData['goods_market'] = $data['goods_market'];
		//商城价
		$goodsData['shop_price'] = $data['shop_price'];
		//默认点击率
		$goodsData['goods_click'] = $data['goods_click'];
		//商品上传时间
		$goodsData['goods_uptime'] = time();
		//添加人ID
		$goodsData['admin_aid'] = session('aid');
		//品牌ID
		$goodsData['goods_brandid'] = $data['goods_brandid'];
		//商品分类ID
		$goodsData['goods_cateid'] = $data['goods_cateid'];
		//商品类型ID
		$goodsData['goods_typeid'] = $data['goods_typeid'];
		//商品列表图
		if (isset($data['goods_listimg'])) {
			$goodsData['goods_listimg'] = $data['goods_listimg'];
		}
		//写入商品表
		$goodsid = $this -> addGoods($goodsData);
		//返回商品ID
		return $goodsid;
	}
	
	
	
	
	/**
	 * 显示所有的列表
	 */
	public function findAllGoods($limit) {
		return $this -> limit($limit) -> all();
	}
	
	
	
	
	
	/***********************前台商品使用*******************************\\
	/**
	 * 显示当前商品
	 */
	 
	 public function showGoods($gid){

		//用于保存当前商品详细信息的数组
		$details = array();
		//返回商品信息
		$details['goods'] = $this->where("gid={$gid}")->find();		
		//返回商品详细信息
		$details['details'] = K('GoodsDetailes')->GoodsDetails($gid);
		//返回商品规格 
		$details['goodsspec'] = K('GoodsAttr')->getGoodsSpec($gid);
		//返回该商品所有的的属性
		$details['goodsattr'] = K('GoodsAttr')->getGoodsSpec($gid,0);
		//返回商品库存，分别的库存
		$details['goodslist'] = K('GoodsList')->where("goods_gid={$gid}")->all();
//		var_dump($details);
		return $details;
	 }
	
	
	/**
	 * 新品上市返回所有的商品按上传时间倒序取12条
	 */
	 public function getNewGoods($limit=12){
	 		
	 	return $this->order('goods_uptime desc')->limit($limit)->all();
		
	 }
	
	
	 /**
	  * 显示一组分类的商品
	  */
	  public function getCateGoods($cates,$limit){
	  	$goods = $this->where("goods_cateid in ({$cates})")->limit($limit)->all();
		return $goods;		
	  }
	  //显示上面分类商品所有的商品数量
	  public function getCateGoodsCount($cates){
	  	$num = $this->where("goods_cateid in ({$cates})")->count();
		return $num;		
	  }
	
	
	}
	
	
	
	
	
	
